package com.towing.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Towing implements EntryPoint {

	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private DBConnectionAsync rpc;
	public Towing() {
		rpc = (DBConnectionAsync) GWT.create(DBConnection.class);
		ServiceDefTarget target = (ServiceDefTarget) rpc;
		// The path 'MySQLConnection' is determined in ./public/LoginScreen.gwt.xml
		// This path directs Tomcat to listen for this context on the server side,
		// thus intercepting the rpc requests.
		String moduleRelativeURL = GWT.getModuleBaseURL() + "MyDBConnection";
		target.setServiceEntryPoint(moduleRelativeURL);

	}
	
	@Override
	public void onModuleLoad() {
		final Button loginButton = new Button("Login");
		final TextBox nameField = new TextBox();
		final PasswordTextBox passwordField = new PasswordTextBox();
		nameField.setText("GWT User");

		// We can add style names to widgets
		loginButton.addStyleName("sendButton");

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel.get("userNameBox").add(nameField);
		RootPanel.get("passwordBox").add(passwordField);
		RootPanel.get("loginButton").add(loginButton);

		// Focus the cursor on the name field when the app loads
		nameField.setFocus(true);
		nameField.selectAll();
		//handler for loginButton
		loginButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				RootPanel.get().clear();
				AsyncCallback<UserBean> callback = new AuthenticationHandler<UserBean>();
				rpc.validateLogin(nameField.getText(), passwordField.getText(), callback);

			}
		});


	}
	private class AuthenticationHandler<T> implements AsyncCallback<UserBean> {
		public void onFailure(Throwable ex) {
			RootPanel.get().add(new HTML("Authentication Failed"));
		}
		public void onSuccess(UserBean result) {
			if (result != null){
				RootPanel.get().clear();
				final DialogBox dialogBox = new DialogBox();
				dialogBox.setAnimationEnabled(true);
				final Button closeButton = new Button("Close");
				dialogBox.setText("Login Succesful");
				VerticalPanel dialogVPanel = new VerticalPanel();
				dialogVPanel.addStyleName("dialogVPanel");
				dialogVPanel.add(new HTML("Now we can move on"));
				dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
				dialogVPanel.add(closeButton);
				dialogBox.setWidget(dialogVPanel);
				dialogBox.center();
				closeButton.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						RootPanel.get().clear();
					}
				});
			} else {
				final DialogBox dialogBox = new DialogBox();
				dialogBox.setAnimationEnabled(true);
				final Button closeButton = new Button("Close");
				dialogBox.setText("Invalid Login");
				VerticalPanel dialogVPanel = new VerticalPanel();
				dialogVPanel.addStyleName("dialogVPanel");
				dialogVPanel.add(new HTML("Try again please."));
				dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
				dialogVPanel.add(closeButton);
				dialogBox.setWidget(dialogVPanel);
				dialogBox.center();
				closeButton.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						RootPanel.get().clear();
					}
				});
			}

		}
	}

}
